package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.LoginBean;
import com.zhongjian.technologycal.mall.http.BaseSubscriber;
import com.zhongjian.technologycal.mall.http.ExceptionHandle;
import com.zhongjian.technologycal.mall.http.MyApiService;
import com.zhongjian.technologycal.mall.http.RetrofitClient;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * Created by wei on 2017/12/14.
 *
 */

public class ChangePasswordActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.change_et_old_password)
    EditText change_et_old_password;
    @BindView(R.id.change_et_new_password)
    EditText change_et_new_password;
    @BindView(R.id.change_et_again_new_password)
    EditText change_et_again_new_password;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("修改密码");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_change_password;
    }
    @Override
    public void initPresenter() {

    }
    @OnClick(R.id.iv_back_page)
    public void onClickBack(){
        finish();
    }

    @OnClick(R.id.change_tv_commit)
    public void onClickChangePasswordCommit(){
        if(TextUtils.isEmpty(change_et_old_password.getText().toString().trim())){
            ToastUitl.showShort("旧密码不能为空～");
            return;

        }else if(TextUtils.isEmpty(change_et_new_password.getText().toString().trim())){
            ToastUitl.showShort("新密码不能为空～");
            return;

        }else if(TextUtils.isEmpty(change_et_again_new_password.getText().toString().trim())){
            ToastUitl.showShort("再次输入密码不能为空～");
            return;

        }else if(!change_et_new_password.getText().toString().trim().equals(change_et_again_new_password.getText().toString().trim())){
            ToastUitl.showShort("两次输入密码不一致～");
            return;
        }else{
            MyApiService service = RetrofitClient.getInstance(ChangePasswordActivity.this).create(MyApiService.class);
            Map<String, String> maps = new HashMap<String, String>();
            maps.put("token", UserUtils.getUserToken());
            maps.put("oldPassword", change_et_old_password.getText().toString().trim());
            maps.put("newPassword", change_et_new_password.getText().toString().trim());
            maps.put("newPasswordAgain", change_et_again_new_password.getText().toString().trim());
            RequestBody body = RequestBody.create(
                    okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

            RetrofitClient.getInstance(ChangePasswordActivity.this).createBaseApi().execute(
                    service.jsonModifyPassword(body), new BaseSubscriber<LoginBean>(ChangePasswordActivity.this) {
                        @Override
                        public void onStart() {
                            super.onStart();
                        }

                        @Override
                        public void onError(ExceptionHandle.ResponeThrowable e) {
                            Log.e("Lyk", e.getMessage());

                        }

                        @Override
                        public void onNext(LoginBean loginBean) {
                            ToastUitl.showShort(loginBean.getError_message());
                            if(loginBean.getError_code()==0){
                                finish();
                            }else if(loginBean.getError_code() == 3){
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            }else{
                                ToastUitl.showShort(loginBean.getError_message());
                            }

                        }
                    });
        }

    }
}
